package com.littleHan.brushQuestions.web.searchTopic.service.impl;

import com.littleHan.brushQuestions.exception.FailedToLoadException;
import com.littleHan.brushQuestions.web.frontPage.dao.FrontPageDao;
import com.littleHan.brushQuestions.web.searchTopic.dao.SearchTopicDao;
import com.littleHan.brushQuestions.web.searchTopic.pojo.vo.DicValueTopic;
import com.littleHan.brushQuestions.web.searchTopic.service.SearchTopicService;
import com.littleHan.brushQuestions.web.start.pojo.DicValue;
import com.littleHan.brushQuestions.web.start.pojo.Topic;
import com.littleHan.brushQuestions.web.uploadTopic.dao.UploadTopicDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SearchTopicServiceImpl implements SearchTopicService {

    @Autowired
    private SearchTopicDao searchTopicDao;

    @Autowired
    private UploadTopicDao uploadTopicDao;

    @Autowired
    private FrontPageDao frontPageDao;

    @Override
    public Map<String,Object> getTopicObjectTypeDifficultList() throws FailedToLoadException {
        Map<String,Object> map=new HashMap<>();
        //获取科目
        List<DicValue> objectList=uploadTopicDao.getObject();
        if(objectList.isEmpty()){
            throw new FailedToLoadException("获取科目失败");
        }
        map.put("topicObjectList",objectList);
        //获取题型
        List<DicValue> typeList=uploadTopicDao.getType();
        if(typeList.isEmpty()){
            throw new FailedToLoadException("获取题型失败");
        }
        map.put("topicTypeList",typeList);
        //获取难度
        List<DicValue> difficultList=uploadTopicDao.getDifficult();
        if(difficultList.isEmpty()){
            throw new FailedToLoadException("获取难度失败");
        }
        map.put("topicDifficultList",difficultList);
        return map;
    }

    @Override
    public Map<String,Object> getSearchTopicList(String userId,DicValueTopic dicValueTopic) {
        //首先根据条件查询题目(会分页)
        List<Topic> topicList=searchTopicDao.getSearchTopicList(dicValueTopic);
        //获取条件查询题目的总数量
        int total=searchTopicDao.getSearchTopicNum(dicValueTopic);
        if(topicList.isEmpty()){
            return null;
        }
        Map<String,Object> map=new HashMap<>();
        map.put("total",total);
        List<DicValueTopic> list=new ArrayList<>();
       for(Topic t:topicList){
           DicValueTopic dicValueTopic1=new DicValueTopic();
           dicValueTopic1.setContent(t.getContent());
           dicValueTopic1.setId(t.getId());
           //题目被收藏的数量
           dicValueTopic1.setCollectNum(frontPageDao.getCollectNumByTopicId(t.getId()));
           //题目被评论的数量
           dicValueTopic1.setCommentNum(frontPageDao.getCommentNumByTopicId(t.getId()));
           //该题目是否被用户收藏了
           int result=searchTopicDao.getCollectInfoByUserIdAndTopicId(userId,t.getId());
           if(result<1){
               dicValueTopic1.setUserIsCollect(false);
           }
           else{
               dicValueTopic1.setUserIsCollect(true);
           }
           list.add(dicValueTopic1);
           map.put("searchResultTopic",list);
       }
       return map;
    }
}
